package thread.callback.guava;

import com.google.common.util.concurrent.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Future {

    private static Logger logger = LoggerFactory.getLogger(Future.class);

    public static void main(String args[]) {
        System.out.println(1L);
        Thread.currentThread().setName("泡茶喝线程");

        //新起一个线程，作为泡茶主线程
        DrinkJob drinkJob = new DrinkJob();

        //烧水的业务逻辑
        Callable<Boolean> hotJob = new HotWaterJob();
        //清洗的业务逻辑
        Callable<Boolean> washJob = new WashJob();

        //创建Java 线程池
        ExecutorService jPool =
                Executors.newFixedThreadPool(10);

        //包装Java线程池，构造guava 线程池
        ListeningExecutorService gPool =
                MoreExecutors.listeningDecorator(jPool);


        //启动烧水线程
        ListenableFuture<Boolean> hotFuture = gPool.submit(hotJob);
        //设置烧水任务的回调钩子
        Futures.addCallback(hotFuture,
                //烧水的回调钩子
                new FutureCallback<Boolean>() {
                    public void onSuccess(Boolean r) {
                        if (r) {
                            drinkJob.waterOk = true;
                            //执行回调方法
                            drinkJob.drinkTea();
                        }
                    }

                    public void onFailure(Throwable t) {
                        logger.info("烧水失败，没有茶喝了");
                    }
                });

        //启动清洗线程
        ListenableFuture<Boolean> washFuture = gPool.submit(washJob);
        //使用匿名实例，作为清洗之后的回调钩子
        Futures.addCallback(washFuture,
                new FutureCallback<Boolean>() {
            public void onSuccess(Boolean r) {
                if (r) {
                    drinkJob.cupOk = true;
                    //执行回调方法
                    drinkJob.drinkTea();
                }
            }

            public void onFailure(Throwable t) {
                logger.info("杯子洗不了，没有茶喝了");
            }
        });

        logger.info("干点其他事情...");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        logger.info("执行完成");
    }
//         package com.crazymakercircle.coccurent;
//    // 省略import
//    public class GuavaFutureDemo

//    {

    public static final int SLEEP_GAP = 3000;

    static class HotWaterJob implements Callable<Boolean> //①
    {
        @Override
        public Boolean call() throws Exception //②
        {
            try {
//                logger.info("洗好水壶");
                logger.info("烧开水");

                //线程睡眠一段时间，代表烧水中
                Thread.sleep(SLEEP_GAP);
                logger.info("水开了");

            } catch (InterruptedException e) {
                logger.info(" 发生异常被中断.");
                return false;
            }
            logger.info(" 烧水工作，运行结束.");

            return true;
        }

    }

    static class WashJob implements Callable<Boolean> {


        @Override
        public Boolean call() throws Exception {
            try {
                logger.info("洗茶杯");
                //线程睡眠一段时间，代表清洗中
                Thread.sleep(SLEEP_GAP+1000);
                logger.info("洗完了");

            } catch (InterruptedException e) {
                logger.info(" 清洗工作发生异常被中断.");
                return false;
            }
            logger.info(" 清洗工作, 运行结束.");
            return true;
        }

    }

    //泡茶喝的工作
    static class DrinkJob {

        boolean waterOk = false;

        boolean cupOk = false;

        //泡茶喝，回调方法
        public void drinkTea() {
            if (waterOk && cupOk) {
                logger.info("泡茶喝，茶喝完");
                this.waterOk = false;
            }

        }

    }

}

//}
